/**
 * 登录用户
 * @author Canaan
 * @date 2019/8/12 11:02
 */
export default {
  state: {
    userContext: {
      id: null,           //主键
      nickname: '',       //呢称
      username: sessionStorage.getItem('x_asia_tasty_login_username'), //账号
      role: sessionStorage.getItem('x_asia_tasty_login_role'),     //角色
      brand: {},       //如果是品牌角色有值
      store: {}        //如果是门店角色有值
    }
  },
  actions: {
    loadUserContext({commit, state}, vue) {
      //sessionStorage 没有token 那么不加载
      const token = sessionStorage.getItem(vue.$env.LOGIN_TOKEN);
      if (!token) {
        return;
      }

      //清空缓存
      commit('clearMaterialCategoryOptions');
      commit('clearMaterialOptions');
      commit('clearMaterialUnitOptions');

      return vue.$ajax.get('user_info/user_context')
        .then(res => {
          if (res.code !== 'ok') {
            throw res.msg;
          }
          commit('userContext', res.data);
          return res.data;
        });
    }
  },
  mutations: {
    userContext({userContext}, loginUser) {
      userContext = Object.assign(userContext, {
        id: null,
        nickname: null,
        username: null,
        role: null,
        brand: {},
        store: {}
      }, loginUser);
    }
  },
  getters: {}

}
